<pre class='metadata'>
Title: CSS Box Model Module Level 3
Shortname: css-box
Level: 3
Status: ED
Prepare for TR: no
Implementation Report: https://wpt.fyi/results/css/css-box
Work Status: Completed
Group: csswg
ED: https://drafts.csswg.org/css-box-3/
TR: https://www.w3.org/TR/css-box-3/
Previous Version: https://www.w3.org/TR/2022/CRD-css-box-3-20221103/
Abstract: This specification describes the margin and padding properties, which create spacing in and around a CSS box.
Editor: Elika J. Etemad / fantasai, Apple, http://fantasai.inkedblade.net/contact, w3cid 35400
Ignored Terms: internal table elements, block layout
Issue Tracking: CSSWG GitHub https://github.com/w3c/csswg-drafts/issues
</pre>
<pre class='link-defaults'>
spec:css-backgrounds-3; type:property; text:border
spec:css-sizing-3; type:dfn; text:size
</pre>

Introduction {#intro}
============

	<p><em>This subsection is not normative.</em>

	<p>CSS describes how each element
	and each string of text in a source document
	is laid out by transforming the <a>element tree</a>
	into a set of <a>boxes</a>,
	whose size, position, and stacking level on the <a>canvas</a>
	depend on the values of their CSS properties.

	Note: <a href="https://www.w3.org/TR/css-cascade/">CSS Cascading and Inheritance</a>
	describes how properties are assigned to elements in the box tree,
	while [[css-display-3#intro]] describes how the <a>element tree</a>
	is transformed into the <a>box tree</a>.

	<p>Each CSS <a>box</a>
	has a rectangular content area,
	a band of padding around the content,
	a border around the padding,
	and a margin outside the border.
	The <a>sizing properties</a> [[css-sizing-3]],
	together with various other properties that control layout,
	define the size of the content area.
	The box styling properties--
	'padding' and its longhands,
	'border' and its longhands,
	and 'margin' and its longhands--
	define the sizes of these other areas.

	[=Margins=] and [=padding=] are defined in this module.
	[=Borders=] are similarly defined in [[css-backgrounds-3]].

	Note: This module only defines the [=physical=] per-side [=longhand=] properties.
	Additional, [=flow-relative=] [=longhand=] properties
	are defined in [[css-logical-1]].
	Note that both sets of [=longhand=] properties control the same [=margins=]/[=padding=]:
	they are just different ways of indexing each side.

	<details class="note">
	<summary>History of CSS Box module and the definition of Block Layout</summary>
	<p>This module <a href="https://www.w3.org/TR/2018/WD-css3-box-20180731/">originally contained</a>
	the CSS Level 3 specification prose relating to
	box generation (now defined in [[css-display-3]]),
	the box model (defined here),
	as well as block layout (now only defined in [[CSS2]] Chapters 9 and 10).
	Since its maintenance was put aside during the development of CSS2.1,
	its prose was severely outdated by the time CSS2 Revision 1
	was finally completed.
	Therefore, the block layout portion of the prose has been retired,
	to be re-synched to <a href="https://www.w3.org/TR/CSS2">CSS2</a> and updated
	as input to a new Block Layout module at some point in the future.
	It is being split apart from this module
	and from the <a href="https://www.w3.org/TR/css-display/">CSS Display Module</a>
	both because of the practical concern that it would be a huge amount of work
	and also in recognition that CSS now has multiple layout models
	(<a href="https://www.w3.org/TR/css-flexbox/">Flex Layout</a>,
	<a href="https://www.w3.org/TR/css-grid/">Grid Layout</a>,
	<a href="https://www.w3.org/TR/css-position/">Positioned Layout</a>,
	and <a href="https://www.w3.org/TR/css-tables/">Table Layout</a>,
	in addition to Block Layout)
	which each deserve their own parallel module.
	</details>

<h3 id="values">
Value Definitions</h3>

	This specification follows the <a href="https://www.w3.org/TR/CSS2/about.html#property-defs">CSS property definition conventions</a> from [[!CSS2]]
	using the <a href="https://www.w3.org/TR/css-values-3/#value-defs">value definition syntax</a> from [[!CSS-VALUES-3]].
	Value types not defined in this specification are defined in CSS Values &amp; Units [[!CSS-VALUES-3]].
	Combination with other CSS modules may expand the definitions of these value types.

	In addition to the property-specific values listed in their definitions,
	all properties defined in this specification
	also accept the <a>CSS-wide keywords</a> as their property value.
	For readability they have not been repeated explicitly.

<h3 id="placement">
Module Interactions</h3>

	<p>This module replaces the definitions of the margin and padding properties
	defined in [[!CSS2]] sections 8.1, 8.2, 8.3 (but not 8.3.1), and 8.4.

	<p>All properties in this module apply to the
	''::first-line'' and ''::first-letter'' pseudo-elements.

The CSS Box Model {#box-model}
=================

	<p export>Each box has a <dfn>content area</dfn>
	(which contains its content--
	text, descendant boxes, an image or other <a>replaced element</a> content, etc.)
	and optional surrounding
	<dfn lt="padding area">padding</dfn>,
	<dfn lt="border area">border</dfn>,
	and <dfn lt="margin area">margin areas</dfn>;
	the size of each area is specified by corresponding properties,
	and can be zero
	(or in the case of margins, negative).
	The following diagram shows how these areas relate
	and the terminology used to refer to the various parts of the box:

	<div class="figure">
		<p><img src="images/box.png" alt="Diagram of a typical box, showing the
		content, padding, border and margin areas" width="461">

		<p class="caption">The various areas and edges of a typical box.
	</div>

	The background of the content, padding, and border areas of a box
	is specified by its 'background' properties.
	The border area can additionally be painted with a border style
	using the 'border' properties.
	Margins are always transparent.
	See [[css-backgrounds-3]].

	The margin, border, and padding can be broken down into
	top, right, bottom, and left segments,
	each of which can be controlled independently
	by its corresponding property.

Boxes and Edges {#box-edges}
---------------

	The perimeter of each of the four areas
	(content, padding, border, and margin)
	is called an <dfn export lt="box edge | edge" for="box">edge</dfn>,
	and each [=edge=] can be broken down into
	a top, right, bottom, and left side.
	Thus each <a>box</a> has four [=edges=]
	each composed of four sides:

	<dl export>
		<dt><dfn>content edge</dfn> or <dfn>inner edge</dfn>
		<dd>
			The content edge surrounds
			the rectangle given by the width and height of the box,
			which often depend on the element's content
			and/or its <a>containing block</a> size.
			The four sides of the <a>content edge</a> together
			define the box's <dfn>content box</dfn>.

		<dt><dfn>padding edge</dfn>
		<dd>
			The padding edge surrounds
			the box’s padding.
			If the padding has zero width on a given side,
			the padding edge coincides with the content edge on that side.
			The four sides of the <a>padding edge</a> together
			define the box's <dfn>padding box</dfn>,
			which contains both the
			<a lt="content area">content</a>
			and <a>padding areas</a>.

		<dt><dfn>border edge</dfn>
		<dd>
			The border edge surrounds the box’s border.
			If the border has zero width on a given side,
			the border edge coincides with the padding edge on that side.
			The four sides of the <a>border edge</a> together
			define the box's <dfn>border box</dfn>,
			which contains the box’s
			<a lt="content area">content</a>,
			<a lt="padding area">padding</a>,
			and <a>border areas</a>.

		<dt><dfn>margin edge</dfn> or <dfn>outer edge</dfn>
		<dd>
			The margin edge surrounds the box’s margin.
			If the margin has zero width on a given side,
			the margin edge coincides with the border edge on that side.
			The four sides of the <a>margin edge</a> together
			define the box's <dfn>margin box</dfn>,
			which contains the all of the box’s
			<a lt="content area">content</a>,
			<a lt="padding area">padding</a>,
			<a lt="border area">border</a>,
			and <a>margin areas</a>.
	</dl>

Fragmentation {#fragmentation}
-------------

	When a box <a href="https://www.w3.org/TR/css-break-3/#fragmentation-model">fragments</a>--
	is broken, as across lines or across pages, into separate <a>box fragments</a>--
	each of its boxes
	(<a>content box</a>, <a>padding box</a>, <a>border box</a>, <a>margin box</a>)
	also fragments.
	How the content/padding/border/margin areas react to fragmentation
	is specified in [[css-break-3]]
	and controlled by the 'box-decoration-break' property.

Box-edge Keywords {#keywords}
-----------------

	The following <dfn><<box>></dfn> CSS keywords are defined for use
	in properties (such as 'transform-box' and 'background-clip')
	that need to refer to various box edges:

	<dl dfn-type=value>
		<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">content-box</dfn>
			<dd>
				Refers to the [=content box=] or [=content edge=].
				(In an SVG context, treated as ''<box>/fill-box''.)

		<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">padding-box</dfn>
			<dd>
				Refers to the [=padding box=] or [=padding edge=].
				(In an SVG context, treated as ''<box>/fill-box''.)

		<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">border-box</dfn>
			<dd>
				Refers to the [=border box=] or [=border edge=].
				(In an SVG context, treated as ''<box>/stroke-box''.)

		<dt><dfn dfn-for="<box>,<layout-box>,<shape-box>,<geometry-box>">margin-box</dfn>
			<dd>
				Refers to the [=margin box=] or [=margin edge=].
				(In an SVG context, treated as ''<box>/stroke-box''.)

		<dt><dfn dfn-for="<box>,<geometry-box>,<paint-box>,<coord-box>">fill-box</dfn>
			<dd>
				Refers to the [=object bounding box=] or its edges.
				(In a CSS box context, treated as ''<box>/content-box''.)

		<dt><dfn dfn-for="<box>,<geometry-box>,<paint-box>,<coord-box>">stroke-box</dfn>
			<dd>
				Refers to the [=stroke bounding box=] or its edges.
				(In a CSS box context, treated as ''<box>/border-box''.)

		<dt><dfn dfn-for="<box>,<geometry-box>,<coord-box>">view-box</dfn>
			<dd>
				Refers to the nearest [=SVG viewport=] element’s
				<dfn dfn lt="SVG viewport origin box" local-lt="origin box">origin box</dfn>,
				which is a rectangle with the width and height
				of the initial SVG <a spec=svg2>user coordinate system</a>
				established by the <{svg/viewBox}> attribute for that element,
				positioned such that its top left corner is anchored
				at the coordinate system origin.
				(In a CSS box context, treated as ''<box>/border-box''.)

				Note: When the <{svg/viewBox}>
				includes non-zero <var ignore>min-x</var> or <var ignore>min-y</var> offsets,
				this [=origin box=] does not actually correspond
				to the visible region rectangle defined by the <{svg/viewBox}>!
				<!-- This is a legacy compat requirement.
				     See https://github.com/w3c/csswg-drafts/issues/4662 -->
	</dl>

	For convenience, the following value types are defined
	to represents commonly used subsets of <<box>>:

	<pre class="prod">
		<dfn><<visual-box>></dfn> = content-box | padding-box | border-box
		<dfn><<layout-box>></dfn> = <<visual-box>> | margin-box
		<dfn><<paint-box>></dfn> = <<visual-box>> | fill-box | stroke-box
		<dfn><<coord-box>></dfn> = <<paint-box>> | view-box
	</pre>

Margins {#margins}
=======

	<dfn export lt="margin">Margins</dfn> surround the border edge of a box,
	providing spacing between boxes.
	The <dfn export>margin properties</dfn> specify the thickness
	of the <a>margin area</a> of a box.
	The 'margin' <a>shorthand property</a>
	sets the margin for all four sides
	while the margin <a>longhand properties</a> only set their respective side.
	This section defines the <a>physical</a> 'margin' <a>longhands</a>.
	(Additional <a>flow-relative</a> 'margin' <a>longhands</a>
	are defined in [[css-logical-1]].)

	Note: Adjoining margins in <a>block layout</a> can <em>collapse</em>.
	See <a href="https://www.w3.org/TR/CSS2/box.html#collapsing-margins">CSS2&sect;8.3.1 Collapsing Margins</a>
	for details.
	Also, margins adjoining a <a>fragmentation break</a> are sometimes truncated.
	See [[css-break-3#break-margins]] for details.

Page-relative (Physical) Margin Properties: the 'margin-top', 'margin-right', 'margin-bottom', and 'margin-left' properties {#margin-physical}
-------------------------------------------

	<pre class="propdef">
		Name: margin-top, margin-right, margin-bottom, margin-left
		Value: <<length-percentage>> | auto
		Initial: 0
		Applies to: all elements except <a>internal table elements</a>
		Inherited: no
		Logical property group: margin
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: the keyword ''margin/auto'' or a computed <<length-percentage>> value
		Animation type: by computed value type
	</pre>

	These properties set the top, right, bottom, and left
	<a>margin</a> of a <a>box</a>, respectively.

	Negative values for margin properties are allowed,
	but there may be implementation-specific limits.

	Note: Application to [=internal ruby boxes=] [[CSS-RUBY-1]]
	is not defined in this specification.

Margin Shorthand: the 'margin' property {#margin-shorthand}
-----------------

	<pre class="propdef">
		Name: margin
		Value: <<'margin-top'>>{1,4}
		Initial: 0
		Applies to: see individual properties
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: see individual properties
		Animation type: by computed value type
	</pre>

	The 'margin' property is a shorthand property for setting
	'margin-top', 'margin-right', 'margin-bottom', and 'margin-left'
	in a single declaration.

	If there is only one component value,
	it applies to all sides.
	If there are two values,
	the top and bottom margins are set to the first value
	and the right and left margins are set to the second.
	If there are three values,
	the top is set to the first value,
	the left and right are set to the second,
	and the bottom is set to the third.
	If there are four values
	they apply to the top, right, bottom, and left, respectively.

	<div class="example">
		The following code demonstrates some possible 'margin' declarations.

		<pre>
			body { margin: 2em }         /* all margins set to 2em */
			body { margin: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
			body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
		</pre>

		The last rule of the example above is equivalent to the example below:
		<pre>
			body {
			  margin-top: 1em;
			  margin-right: 2em;
			  margin-bottom: 3em;
			  margin-left: 2em; /* copied from opposite side (right) */
			}
		</pre>
	</div>

Padding {#paddings}
=======

	<dfn export>Padding</dfn> is inserted between the content edge
	and the padding edge of a box,
	providing spacing between the content and the border.
	The <dfn export>padding properties</dfn> specify the thickness
	of the <a>padding area</a> of a box.
	The 'padding' <a>shorthand property</a>
	sets the padding for all four sides
	while the padding <a>longhand properties</a> only set their respective side.
	This section defines the <a>physical</a> 'padding' <a>longhands</a>.
	(Additional <a>flow-relative</a> 'padding' <a>longhands</a>
	are defined in [[css-logical-1]].)

	Note: Backgrounds specified on the box
	are by default laid out and painted within the padding edges.
	(They are additionally painted underneath the border,
	in the <a>border area</a>.)
	This behavior can be adjusted
	using the 'background-origin' and 'background-clip' properties.

Page-relative (Physical) Padding Properties: the 'padding-top', 'padding-right', 'padding-bottom', and 'padding-left' properties {#padding-physical}
-------------------------------------------

	<pre class="propdef">
		Name: padding-top, padding-right, padding-bottom, padding-left
		Value: <<length-percentage [0,∞]>>
		Initial: 0
		Applies to: all elements except: <a>internal table elements</a> other than table cells
		Inherited: no
		Logical property group: padding
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: a computed <<length-percentage>> value
		Animation type: by computed value type
	</pre>

	These properties set the top, right, bottom, and left
	<a>padding</a> of a <a>box</a>, respectively.

	Negative values for padding properties are invalid.

	Note: Application to [=internal ruby boxes=] [[CSS-RUBY-1]]
	is not defined in this specification.

Padding Shorthand: the 'padding' property {#padding-shorthand}
-----------------

	<pre class="propdef">
		Name: padding
		Value: <<'padding-top'>>{1,4}
		Initial: 0
		Applies to: see individual properties
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: see individual properties
		Animation type: by computed value type
	</pre>

	The 'padding' property is a shorthand property for setting
	'padding-top', 'padding-right', 'padding-bottom', and 'padding-left'
	in a single declaration.

	If there is only one component value,
	it applies to all sides.
	If there are two values,
	the top and bottom padding are set to the first value
	and the right and left padding are set to the second.
	If there are three values,
	the top is set to the first value,
	the left and right are set to the second,
	and the bottom is set to the third.

	<div class="example">
		The following code demonstrates some possible 'padding' declarations.

		<pre>
			body { padding: 2em }         /* all padding set to 2em */
			body { padding: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
			body { padding: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
		</pre>

		The last rule of the example above is equivalent to the example below:
		<pre>
			body {
			  padding-top: 1em;
			  padding-right: 2em;
			  padding-bottom: 3em;
			  padding-left: 2em; /* copied from opposite side (right) */
			}
		</pre>
	</div>

Borders {#borders}
=======

	<dfn export lt="border">Borders</dfn> fill the <a>border area</a>,
	to visually delineate the edges of the box,
	The <dfn export>border properties</dfn> specify the thickness
	of the <a>border area</a> of a box,
	as well as its drawing style and color.
	See [[css-backgrounds-3]] for the definition of these properties,
	including their [=physical=] longhands.
	(Additional [=flow-relative=] border longhands
	are defined in [[css-logical-1]].)


Changes {#changes}
=======

Recent Changes {#changes-recent}
--------------

	There have only been minor editorial adjustments
	since the <a href="https://www.w3.org/TR/css-box-3/">6 April 2023 Recommendation</a>:

	* Adding some subheadings to [[#box-model]] and moving a paragraph accordingly.
	* Changing “document tree” to “element tree” in the [[#intro]]
		to align with the terminology used in [[css-display-3]].
	* Adding the “Logical Property Group” line to the property definition tables.

Changes Since CSS Level 2 {#changes-since-L2}
-------------------------

	The following changes have been made to this module
	since <a href="https://www.w3.org/TR/CSS2/box.html">CSS Level 2</a>:
	<ul>
		<li>Marked application to internal ruby boxes as undefined in this level.</li>
		<li>Adapting the prose slightly to account for vertical <a>writing modes</a>.
		<li>Cross-linking to relevant concepts in [[css-break-3]] and [[css-backgrounds-3]].
		<li>Providing a [[#keywords|centralized common definition]]
			of keywords that reference the various box edges
			for use in properties defined outside this spec
			(e.g. 'background-clip')
			to avoid repetition (and the resulting inevitable synchronization errors).
	</ul>

Privacy Considerations {#priv}
===============================================

	No privacy considerations have been reported on this module.

Security Considerations {#sec}
===============================================

	No security considerations have been reported on this module.


